package luj.tool.jnproxyan.proxy.util.data;

import luj.data.api.Data;
import luj.data.api.DataContext;
import luj.tool.jnproxyan.proxy.database.DatabaseState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicLong;

@Component
public class DataNewCreator {

  public DataNewCreator inject(Class<?> dataType, DatabaseState dbState) {
    DataNewCreator result = new DataNewCreator();
    result._dataType = dataType;
    result._dbState = dbState;

    result._lujdata = _lujdata;
    return result;
  }

  public Data create() {
    AtomicLong nextId = _dbState.getNextId();
    Long dataId = nextId.getAndIncrement();

    Data data = _lujdata.createData(dataId);
    data.setType(_dbState.getDataTypes().getType(_dataType));

    return data;
  }

  Class<?> _dataType;
  DatabaseState _dbState;

  @Autowired
  DataContext _lujdata;
}
